package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.AlarmConfigBean;
import com.emip.dao.AlarmConfigDAO;
import com.emip.dao.BaseDAO;

@Repository("alarmConfigDAO")
public class AlarmConfigDAOImpl extends BaseDAO implements AlarmConfigDAO {

	public int add(AlarmConfigBean entity) {
		Integer id = (Integer)this.getHibernateTemplate().save(entity);
		return id.intValue();
	}

	public AlarmConfigBean findByID(Integer ID) {
		AlarmConfigBean bean = this.getHibernateTemplate().get(AlarmConfigBean.class, ID);
		return bean;
	}

	public List<AlarmConfigBean> getAll() {
		List<AlarmConfigBean> result = this.getHibernateTemplate().find("from AlarmConfigBean m order by m.id");
		return result;
	}

	public void remove(AlarmConfigBean entity) {
		this.getHibernateTemplate().delete(entity);
	}

	public void update(AlarmConfigBean entity) {
		this.getHibernateTemplate().update(entity);
	}

	public Boolean findExist(AlarmConfigBean entity) {
		Object[] params = new Object[] {entity.getAlarmID(), entity.getAlarmName()};
		List<AlarmConfigBean> result = this.getHibernateTemplate().find("from AlarmConfigBean m where alarmID=? and alarmName=?", params);
		if (result.size()==0) {
			return false;
		}
		else {
			//是否存在的那条记录的alarmID 和 alarmName 和自己是相同的，如果是的话，那么其实是update其他字段的一种操作，也是合法操作
			if (entity.getId()==0) {  //新增的情况
				return true;
			} else {
				AlarmConfigBean eBean = this.getHibernateTemplate().get(AlarmConfigBean.class, entity.getId());
				if (entity.getAlarmID().equals(eBean.getAlarmID()) &&
						entity.getAlarmName().equals(eBean.getAlarmName())) {
					return false;
				} else {
					return true;
				}
			}
		}
	}

}
